package com.parkreport.admin.areacity.service;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.common.base.BaseService;
import com.common.jdbc.DB4ObjUtil;
import com.common.jdbc.DBBaseUtil;
import com.common.pager.PageHelper;
import com.common.util.StringUtil;
import com.parkreport.vo.LareaVO;
import com.parkreport.vo.ParkArea;

public class AreaCityService extends BaseService {

	public List find(LareaVO vo,PageHelper pager){
		StringBuilder sql = new StringBuilder("select * from LAREA where 1=1 ");
		if(vo!=null){
			if(!StringUtil.isBlank(vo.getName())){
				sql.append(" and NAME like '%"+vo.getName()+"%'");
			}
			
		}
		
		
		List<LareaVO> list = DB4ObjUtil.executePager4Obj(new LareaVO(), sql.toString(),pager);
		
		return list;
	}
	public List find(){
		StringBuilder sql = new StringBuilder("select * from LAREA where 1=1 ");
	
		
		
		List<LareaVO> list = DB4ObjUtil.execute4Obj(new LareaVO(), sql.toString());
		
		return list;
	}
	public String getTree(){
		List<LareaVO> list = find();
		
		Document doc = DocumentHelper.createDocument();
		Element tree = doc.addElement("item");
		tree.addAttribute("id","0");
		for (int i = 0; i < list.size(); i++) {
			Element e = tree.addElement("item");
			 e.addAttribute("id", String.valueOf(list.get(i).getId()));
			 e.addAttribute("text", list.get(i).getName());
			
			
		}
		
		return doc.asXML();
	}
	public LareaVO get(String id){
		String sql = "select * from LAREA where 1=1 and ID=?";
		List<LareaVO> list = DB4ObjUtil.execute4Obj(new LareaVO(), sql, id);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
	}
	

	public void delete(String ids){
		String sql = "delete from LAREA where ID=?";
		
		if(!StringUtil.isBlank(ids)){
			String[] idArr = ids.split(",");
			DBBaseUtil.executeBatchUpdate(sql, idArr);
			
		}
	}
	
	public void delete(String[] ids){
		String sql = "delete from LAREA where ID=?";
		
		if(!StringUtil.isBlank(ids)){
			DBBaseUtil.executeBatchUpdate(sql, ids);
			
		}
		
		
	}
	public String save(LareaVO vo){
		if(vo!=null){
			String isHave = isApply(vo);
			if("0".equals(isHave)){
				if(StringUtil.isBlank(vo.getId())){
						String id = StringUtil.getUuid();
						String sql = "insert into LAREA(ID,NAME) values(?,?)";
						DBBaseUtil.executeUpdate(sql,id, vo.getName());
				}else{
					
					String sql = "update LAREA set NAME=?  where ID=?";
					DBBaseUtil.executeUpdate(sql, vo.getName(),vo.getId());
				}
			}else{
				return "0";
			}
		}
		return "1";
	}
	
	public String isApply(String carNum,String id){
		String sql = "select * from LAREA where 1=1 and NAME=? and ID<>?";
		List<LareaVO> list = DB4ObjUtil.execute4Obj(new LareaVO(), sql, carNum,id);
		if(list.size()>0){
			return "1";
		}
		return "0";
	}
	
	public String isApply(LareaVO vo){
		String sql = "select * from LAREA where 1=1 and NAME=? and ID<>?";
		List<LareaVO> list = DB4ObjUtil.execute4Obj(new LareaVO(), sql, vo.getName(),vo.getId());
		if(list.size()>0){
			return "1";
		}
		return "0";
	}
}
